package cn.gene.timeyan.ums.webapi.ums.controller;

import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.ums.service.ICollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 收藏    前端控制器*
 *
 * @author LWG
 * @date 2022/7/23 11:03
 * @since 1.0.0@gene.cn
 */
@Slf4j
@RestController
@RequestMapping("/ums/Collect")
@Api(tags="收藏模块")
public class CollectController {
    @Autowired
    private ICollectService collectService;
    @ApiOperation("收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID",name = "userId",dataType = "Long",
                    required = true),
            @ApiImplicitParam(value = "帖子ID",name = "articleId",dataType = "Long",
                    required = true),
    })
    @PostMapping("/collection")
    public JsonResult collection(
            @RequestParam(value = "userId",defaultValue = "1L")Long userId,
            @RequestParam(value = "articleId",defaultValue = "2L") Long articleId
    ){
        if(userId==null||articleId==null) {
            return JsonResult.ok("数据不能为空");
        }
        collectService.addCollecton(userId, articleId);
        return JsonResult.ok("收藏成功");
    }

    @ApiOperation("取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID",name = "userId",dataType = "Long",
                    required = true),
            @ApiImplicitParam(value = "帖子ID",name = "articleId",dataType = "Long",
                    required = true),
    })
    @PostMapping("/notCollection")
    public JsonResult attention(
            @RequestParam(value = "userId",defaultValue = "1L")Long userId,
            @RequestParam(value = "articleId",defaultValue = "2L") Long articleId
    ){
        if(userId==null||articleId==null) {
            return JsonResult.ok("数据不能为空");
        }

        collectService.deleteCollection(userId, articleId);
        return JsonResult.ok("取消收藏成功");
    }

    @ApiOperation("收藏数")
    @GetMapping("/getCollectionNum")
    public JsonResult<Integer> getCollectionNum(Long articleId){
        Integer collectionNum=collectService.getCollectionNum(articleId);
        log.info("collection{}",collectionNum);
        return JsonResult.ok(collectionNum);
    }

}
